package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.domain.Brand;
import cn.wolfcode.wms.qo.OrederChartQuery;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.qo.SaleChartQuery;
import cn.wolfcode.wms.service.IBrandService;
import cn.wolfcode.wms.service.IChartService;
import cn.wolfcode.wms.service.IClientService;
import cn.wolfcode.wms.service.ISupplierService;
import cn.wolfcode.wms.util.Dictonary;
import cn.wolfcode.wms.util.JSONResult;
import cn.wolfcode.wms.util.PageResult;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("chart")
public class ChartController {
    @Autowired
    private IBrandService brandService;
    //供应商
    @Autowired
    private ISupplierService supplierService;
    @Autowired
    private IChartService chartService;
    //客户
    @Autowired
    private IClientService clientService;



    @RequestMapping("order")
    public String list(Model model, @ModelAttribute("qo") OrederChartQuery qo){
        List<Map<String, Object>> list = chartService.queryOrderChart(qo);
        //列表
        model.addAttribute("list",list);
        //供应商
        model.addAttribute("suppliers",supplierService.selectall());
        //品牌
        model.addAttribute("brands",brandService.selectall());
        //分组
        model.addAttribute("orders", Dictonary.ORDER_MAP);
        return "chart/list";
    }

    @RequestMapping("sale")
    public String sale(Model model, @ModelAttribute("qo") SaleChartQuery qo){
        List<Map<String, Object>> list = chartService.querySaleChart(qo);
        //列表
        model.addAttribute("list",list);

        //品牌
        model.addAttribute("brands",brandService.selectall());
        //分组
        model.addAttribute("sales", Dictonary.SALE_MAP);
        //客户
        model.addAttribute("clients", clientService.selectall());
        return "chart/sale";
    }

    @RequestMapping("saleByBar")
    public String saleByBar(Model model, @ModelAttribute("qo") SaleChartQuery qo) throws JsonProcessingException {
       //需要查询的数据使用json返回,返回的其实是一个数组,可以在页面上直接显示,不需要用key来取
        //groupType 和通过数据查出groupType和销售总额对应的值
        String groupType = qo.getGroupType();//这里显示的e.name之类的 通过字典转换
        model.addAttribute("groupType",Dictonary.SALE_MAP.get(groupType));
        List<Map<String, Object>> maps = chartService.querySaleChart(qo);
        //如果这个list直接转换json是有key值的,需要的返回只要数组[...]
        List<Object> groupTypes= new ArrayList<>();
        List<Object> totalAmount= new ArrayList<>();
        for (Map<String, Object> map : maps) {
           groupTypes.add( map.get("groupType"));
           totalAmount.add(map.get("totalAmount"));
        }
        //X轴显示的数据 ["x","y","z"]
        ObjectMapper JSON = new ObjectMapper();
        String g = JSON.writeValueAsString(groupTypes);
        String amount = JSON.writeValueAsString(totalAmount);

        model.addAttribute("groupTypes",g);
        model.addAttribute("totalAmount",amount);

        return "chart/saleByBar";
    }


    @RequestMapping("saleByPie")
    public String saleByPie(Model model, @ModelAttribute("qo") SaleChartQuery qo) throws JsonProcessingException {
        //需要查询的数据使用json返回,返回的其实是一个数组,可以在页面上直接显示,不需要用key来取
        //groupType 和通过数据查出groupType和销售总额对应的值
        String groupType = qo.getGroupType();//这里显示的e.name之类的 通过字典转换
        model.addAttribute("groupType",Dictonary.SALE_MAP.get(groupType));
        List<Map<String, Object>> maps = chartService.querySaleChart(qo);
        //如果这个list直接转换json是有key值的,需要的返回只要数组[...]
        List<Map<String, Object>> newMaps = new ArrayList<>();
        //饼状图的数据[{name:"xx",value:100},{name:"00",value:50}]
//        {totalNumber=785.00, totalAmount=1668600.00, groupType=admin, profit=476200.00}
//        {totalNumber=180.00, totalAmount=288000.00, groupType=李四明, profit=86400.00}
        List<Object> groupTypes= new ArrayList<>();
        for (Map<String, Object> map : maps) {
            groupTypes.add( map.get("groupType"));
            Map<String, Object> m = new HashMap<>();
            m.put("name",map.get("groupType"));
            m.put("value",map.get("totalAmount"));

            newMaps.add(m);
        }
        ObjectMapper JSON = new ObjectMapper();
        String s = JSON.writeValueAsString(newMaps);
        newMaps.forEach(System.out::println);
        String g = JSON.writeValueAsString(groupTypes);
        model.addAttribute("data",s);
        model.addAttribute("groupTypes",g);



        return "chart/saleByPie";
    }


}
